####
Run()
{
   echo "Run $@"
   $@
   if [ $? -ne 0 ];then
      echo "Failed: [$@]"
      exit 0
   fi
}

APP_NAME=zerotier
DEPLOY_DIR=/mnt/HD/HD_a2/app/$APP_NAME
VERSION=1.4.6
ARCH=armhf
BIN_NAME=zerotier-one

echo "try to find the lastest ..."
LASTEST_VERSION=`curl -s https://download.zerotier.com/RELEASES/|grep -oe '[1-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*'|sort|tail -1`
if [ -n "$LASTEST_VERSION" ];then
	VERSION=$LASTEST_VERSION
	echo "found: $LASTEST_VERSION"
else
    echo "not found, now it would use $VERSION"
fi
echo ""

###create the work directory which needed
for dir in bin webpage cgi
do
   if [ ! -d $DEPLOY_DIR/$dir ];then
      Run mkdir -p $DEPLOY_DIR/$dir
   fi
done

##download binrary
NeedUpdate=1
if [ -f $DEPLOY_DIR/bin/$BIN_NAME ];then
   CURR_VERSION=`$DEPLOY_DIR/bin/$BIN_NAME -v`
   if [ "$CURR_VERSION" = "$VERSION" ];then
      echo "The lastest version[$VERSION] was same as current version[$CURR_VERSION]"
	  echo "not need to update binrary"
	  NeedUpdate=0
   fi
fi

if [ $NeedUpdate -eq 1 ];then
   Run cd $DEPLOY_DIR/bin
   echo "try to download ..."
   Run wget --no-check-certificate https://download.zerotier.com/RELEASES/${VERSION}/dist/static-binaries/zerotier-one.${ARCH}.gz
   echo "ok"
   Run gunzip -f zerotier-one.${ARCH}.gz
   if cmp -s zerotier-one.${ARCH} $BIN_NAME ;then
      #the binrary was same, not need to update. delete the downloaded file
      Run rm -f zerotier-one.${ARCH}
      echo "the binrary was same, not need to update"
   else
      #file was different need to update
      if [ -f $BIN_NAME ];then
          Run mv $BIN_NAME $BIN_NAME.old
      fi
      Run mv zerotier-one.${ARCH} $BIN_NAME 
      echo "updated the $BIN_NAME"
   fi
   Run chmod +x $BIN_NAME
fi

##create link
if [ ! -f /usr/bin/zerotier-one ];then
  Run ln -s $DEPLOY_DIR/bin/zerotier-one /usr/bin/zerotier-one
  Run ln -s $DEPLOY_DIR/bin/zerotier-one /usr/bin/zerotier-cli
  Run ln -s $DEPLOY_DIR/bin/zerotier-one /usr/bin/zerotier-idtool
  Run ln -s $DEPLOY_DIR/ /var/lib/zerotier-one
  Run ln -s $DEPLOY_DIR/webpage /var/www/zerotier
  Run ln -s $DEPLOY_DIR/cgi/*   /var/www/cgi-bin/root
fi

Run cd $DEPLOY_DIR
cat<<EOF > $DEPLOY_DIR/$APP_NAME.start
if [ ! -f /usr/bin/zerotier-one ];then
  ln -s $DEPLOY_DIR/bin/zerotier-one /usr/bin/zerotier-one
  ln -s $DEPLOY_DIR/bin/zerotier-one /usr/bin/zerotier-cli
  ln -s $DEPLOY_DIR/bin/zerotier-one /usr/bin/zerotier-idtool
  ln -s $DEPLOY_DIR/ /var/lib/zerotier-one
  ln -s $DEPLOY_DIR/webpage /var/www/zerotier
  ln -s $DEPLOY_DIR/cgi/*   /var/www/cgi-bin/root
fi

pid=\`cat $DEPLOY_DIR/zerotier-one.pid 2>/dev/null\`
if kill -0 \$pid 2>/dev/null; then
   echo "$APP_NAME already started with pid[\$pid]"
else
   echo "$APP_NAME start..."
   zerotier-one -droot
   if [ \$? -eq 0 ];then
      echo "ok"
   else
      echo "nok"
   fi
fi

EOF

cat<<EOF > $DEPLOY_DIR/$APP_NAME.stop
pid=\`cat $DEPLOY_DIR/zerotier-one.pid 2>/dev/null\`
if kill -0 \$pid 2>/dev/null; then
  echo "killing $APP_NAME..."
  kill \$pid
  if [ \$? -eq 0 ];then
    echo "ok"
  else
    echo "nok"
  fi
else
  echo "$APP_NAME already stopped"
fi

EOF

Run chmod +x $DEPLOY_DIR/$APP_NAME.start $DEPLOY_DIR/$APP_NAME.stop
cat<<EOF
Usage:
start zerotier: 
$DEPLOY_DIR/$APP_NAME.start

stop zerotier:
$DEPLOY_DIR/$APP_NAME.stop
EOF


##zerotier-one -droot
##zerotier-cli info
##zerotier-cli join 1111111111111111 #network id

